/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Diagnostics;
using System.Threading;
using System.Timers;
using System.Runtime.Serialization;

namespace SurveillanceDotNet.Model
{
    [Serializable]
    public class RemoteEMailControl : PropertyChangedBase, ISerializable
    {
        private System.Timers.Timer _UpdateTimer;

        public double UpdateTimerInterval { get; set; }
        public MailAccount MailAccount { get; set; }

        private string _Status;
        public string Status
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
                OnPropertyChanged("Status");
            }
        }

        public RemoteEMailControl()
        {
            _UpdateTimer = new System.Timers.Timer();
            _UpdateTimer.Elapsed += new ElapsedEventHandler(_UpdateTimer_Elapsed);
            UpdateTimerInterval = 60000;
            Status = "Stopped";
        }

        public void Start()
        {
            _UpdateTimer.Interval = UpdateTimerInterval;
            _UpdateTimer.Start();

            MailAccount.NewMail += new NewMailEventHandler(MailAccount_NewMail);

            Status = "Running";
        }

        public void Stop()
        {
            _UpdateTimer.Stop();
            MailAccount.NewMail -= MailAccount_NewMail;
            Status = "Stopped";
        }

        void _UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Debug.WriteLine(DateTime.Now.ToString() + " - Checking for new mail");
            MailAccount.CheckNewMail();
        }

        void MailAccount_NewMail(object sender, NewMailEventArgs e)
        {
            string mailContents = (e.Message.Subject + e.Message.BodyText.Text).ToUpper();

            foreach (AlarmAction action in Database.Instance.AlarmActions)
            {
                if (mailContents.Contains(action.Code.ToUpper()))
                {
                    Debug.WriteLine(DateTime.Now.ToString() + " - Invoking action: " + action.Name);
                    new Thread(new ThreadStart(action.Action)).Start();
                }
            }
        }

        protected RemoteEMailControl(SerializationInfo info, StreamingContext context)
        {
            _UpdateTimer = new System.Timers.Timer();
            _UpdateTimer.Elapsed += new ElapsedEventHandler(_UpdateTimer_Elapsed);

            MailAccount = (MailAccount)info.GetValue("MailAccount", typeof(MailAccount));
            UpdateTimerInterval = info.GetInt32("UpdateTimerInterval");

            Status = "Stopped";
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("MailAccount", MailAccount);
            info.AddValue("UpdateTimerInterval", UpdateTimerInterval);
        }
    }
}
